# !/usr/bin/env python
# coding: utf8

import pygtk
pygtk.require('2.0')
import gtk
from manager.process_file_manager      import ProcessFileManager
from manager.plugin_manager      import PluginManager
from manager.bean_store      import *
from manager.configuration_manager      import ConfigurationManager
from computor.write_line_computor      import WriteLineComputor

class MainWindow:

    def showFileChooserEvent(self, widget, donnees=None):
        chooser = gtk.FileChooserDialog(title=None,action=gtk.FILE_CHOOSER_ACTION_OPEN,
                                  buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
        response = chooser.run()
        filename = None
        if response == gtk.RESPONSE_OK:
            filename = chooser.get_filename()
        chooser.destroy()
        self.fileEntry.set_text(filename)
        
    def startProcessEvent(self, widget, donnees=None):
        
        self.stateLabel.set_text("Analyse en cours ...")
        getBean("configurationManager").reload()
        processFileManager = ProcessFileManager()
        processFileManager.registerDocument(self.fileEntry.get_text())
        
        for  plugin in getBean("pluginManager").getInstalledPlugins():
            pluginCheckBox = self.pluginsCheckBoxesDic[plugin.getName()]
            # add plugin computor if plugin is active
            if pluginCheckBox.get_active():
                processFileManager.registerComputor(plugin.getComputor())
        processFileManager.processDocuments()
        self.stateLabel.set_text("")
    
    def showConfigureThematicEvent(self, widget, donnees=None):
            configDialog = ConfigureThematicWindow(self.window)




    def destroy(self, widget, donnees=None):
        gtk.main_quit()

    def __init__(self):
        # création d'une nouvelle fenêtre
        self.pluginsCheckBoxesDic = {}
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(10)
        self.window.set_title("Text-Mining")
        self.window.set_default_size(360, 180)

        #main box
        mainBox = gtk.VBox(False, 0)

        for  plugin in getBean("pluginManager").getInstalledPlugins():
            pluginBox = gtk.HBox(False, 0)
            pluginCheckBox = gtk.CheckButton(plugin.getName())
            pluginCheckBox.show()
            # register checkbox plugin
            self.pluginsCheckBoxesDic[plugin.getName()] = pluginCheckBox
            pluginBox.pack_start(pluginCheckBox, True, True, 0)
            
            if plugin.isConfigurable():
                configurePluginButton = gtk.Button("Configurer")
                configurePluginButton.connect("clicked", plugin.getConfigWindow, None) 
       
                configurePluginButton.show()
                pluginBox.pack_start(configurePluginButton, True, True, 0)
            pluginBox.show()
            mainBox.pack_start(pluginBox, True, False, 0)
                   
        # buttons
        
        self.startProcessButton = gtk.Button("Démarrer")
        self.browseButton = gtk.Button("Parcourir")

        self.startProcessButton.connect("clicked", self.startProcessEvent, None)
        self.browseButton.connect("clicked", self.showFileChooserEvent, None)
        
        self.browseButton.show()
        self.startProcessButton.show()

        # entries
        self.fileEntry = gtk.Entry()
        self.fileEntry.set_editable(False)
        self.fileEntry.show()
        
        #labels
        self.stateLabel = gtk.Label("")
        self.stateLabel.show()
        
        
        fileSelectorBox = gtk.HBox(False, 0)
        fileSelectorBox.show()
        mainBox.show()
        

        fileSelectorBox.pack_start(self.fileEntry, True, True, 0)
        fileSelectorBox.pack_start(self.browseButton, False, False, 0)
        
        mainBox.pack_start(fileSelectorBox, True, False, 0)
        mainBox.pack_start(self.startProcessButton, True, False, 0)
        
        

        
        self.window.add(mainBox)
        self.window.show()


    def loop(self):
        gtk.main()

if __name__ == "__main__":
    registerBean(ConfigurationManager(), "configurationManager")
    registerBean(PluginManager(), "pluginManager")
    mainWindow = MainWindow()
    mainWindow.loop()
